﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using DunSilverlight.Comm;

namespace DunSilverlight.Views
{
    public partial class PhoneCallStatistic : Page
    {
        ReportService.ReportServiceClient ser = new ReportService.ReportServiceClient();
        BankService.BankServiceClient bankSer = new BankService.BankServiceClient();
        public PhoneCallStatistic()
        {
            if (!RightManagement.VerifyRight(RightManagement.Right_Report_PhoneCallStatistic))
            {
                if (AppGlobal.CurrentUser != null)
                    Tools.ShowMessage("该用户没有权限查看 ", "", false);
                return;
            }
            InitializeComponent();

            timePeriod.SelectedDate = DateTime.Now;
            timePeriod.Culture.DateTimeFormat.ShortDatePattern = "yyyy-MM";
            DateTime now = timePeriod.SelectedDate.Value;
            timeStart.SelectedDate = new DateTime(now.Year, now.Month,1);
            timeEnd.SelectedDate = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));

            ser.GetPhoneCallStatisticReportCompleted += new EventHandler<ReportService.GetPhoneCallStatisticReportCompletedEventArgs>(ser_GetPhoneCallStatisticReportCompleted);
            bankSer.GetListByHQLCompleted += new EventHandler<BankService.GetListByHQLCompletedEventArgs>(bankSer_GetListByHQLCompleted);
            bankSer.GetListByHQLAsync("From Company");
        
        }

      

        IList<DunLibrary.Client.Company> bankList;
        void bankSer_GetListByHQLCompleted(object sender, BankService.GetListByHQLCompletedEventArgs e)
        {
            int total = 0;
            bankList = JsonSerializerHelper.JsonToEntities<DunLibrary.Client.Company>(e.Result, out total);
            cboxBank.ItemsSource = bankList;

        }

        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            if (timePeriod.SelectedDate == null)
            {
                Tools.ShowMessage("请选择数据期号!","",false);
                return;
            }
            if (timeStart.SelectedDate == null)
            {
                Tools.ShowMessage("请选择统计日期!","", false);
                return;
            }
            if (timeEnd.SelectedDate == null)
            {
                Tools.ShowMessage("请选择统计日期!", "", false);
                return;
            }
            if(cboxBank.SelectedItem==null)
            {
                Tools.ShowMessage("请选择银行分类!", "", false);
                return;
            }

            if (cboxBank.SelectedItem == null && !string.IsNullOrEmpty(cboxBank.Text))
            {
                Tools.ShowMessage("银行分类输入错误!", "", false);
                return;
            }

            DateTime selected = timePeriod.SelectedDate.Value;
            int period = int.Parse(selected.ToString("yyyyMM"));
            string bankNum = ( cboxBank.SelectedItem as DunLibrary.Client.Company).FullNumber;

            int? import = null;
            if (timeImport.SelectedDate != null)
                import = int.Parse(timeImport.SelectedDate.Value.ToString("yyyyMMdd"));

            DateTime start = dateTransfer(timeStart.SelectedDate.Value, true);
            DateTime end = dateTransfer(timeEnd.SelectedDate.Value, false);

            string orgNum = null;
            if (AppGlobal.CurrentUser.Organization != null)
                orgNum = AppGlobal.CurrentUser.Organization.FullNumber;
            Tools.ShowMask(true);
            ser.GetPhoneCallStatisticReportAsync(period,import, start, end, bankNum, orgNum);
        }

        void ser_GetPhoneCallStatisticReportCompleted(object sender, ReportService.GetPhoneCallStatisticReportCompletedEventArgs e)
        {
            int total = 0;
            var list = JsonSerializerHelper.JsonToEntities<DunLibrary.Report.PhoneCallStatistic>(e.Result, out total);
            RadGridView1.ItemsSource = list;
            Tools.ShowMask(false);
        }

        /// <summary>
        /// 重置所有条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            timePeriod.SelectedDate = DateTime.Now;
            timePeriod.Culture.DateTimeFormat.ShortDatePattern = "yyyy-MM";

            //timeStart.SelectedDate = timeEnd.SelectedDate = null;


            if (cboxBank.SelectedItem != null)
                cboxBank.SelectedItem = null;
            else
                cboxBank.Text = null;

            RadGridView1.ItemsSource = null;
        }

        private void timePeriod_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (timePeriod.SelectedDate != null)
            {
                DateTime now = timePeriod.SelectedDate.Value;
                timeStart.SelectedDate = new DateTime(now.Year, now.Month, 1);
                timeEnd.SelectedDate = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
            }
        }


        /// <summary>
        /// 将控件返回的短日期（yyyy/MM/dd）转换成长日期（yyyy/MM/dd hh:mm:ss）
        /// </summary>
        /// <param name="shortDate"></param>
        /// <param name="isFirst"></param>
        /// <returns></returns>
        private DateTime dateTransfer(DateTime shortDate,bool isFirst)
        {
            DateTime longDate;
            int year = shortDate.Year;
            int month = shortDate.Month;
            int day = shortDate.Day;
            if (isFirst)
            {
                longDate = new DateTime(year, month, day, 0, 0, 0);
            }
            else
            {
                longDate = new DateTime(year, month, day, 23, 59, 59);
            }

            return longDate;
        }

        private void date_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnQuery_Click(sender, null);
            }
        }
    }
}
